var express = require("express");
var router = express.Router();
var moment = require("moment");
const Nav = require("../db/model/navModel");

/**
 * 分页查询轮播图接口
 * @api {POST} /nav/queryNavList 分页查询轮播图接口
 * @apiDescription 分页查询轮播图接口
 * @apiName queryNavList
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiBody {Number} type=1   图片类型
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *        "type": 1
 *     }
 * @apiGroup Nav
 * @apiVersion 1.0.0
 */
router.post("/nav/queryNavList", async function (req, res) {
  const { page, limit, type } = req.body;
  if (!page) {
    return res.send({ code: 500, message: "缺少page参数" });
  }
  if (!limit) {
    return res.send({ code: 500, message: "缺少limit参数" });
  }
  const where = {};
  let offset = (page - 1) * limit;
  if (type != null && type >= 0) {
    where["type"] = type;
  }
  try {
    const result = await Nav.findAndCountAll({
      limit,
      offset,
      where,
    });
    return res.json({ code: 200, message: "success", data: result });
  } catch (error) {
    console.log(error);
    return res.json({ code: -1, message: error });
  }
});

/**
 * @api {POST} /nav/add 轮播图添加
 * @apiDescription 轮播图添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} title=1   标题
 * @apiBody {Number} img_url=10   图片url
 * @apiBody {Number} type=1   图片类型
 * @apiBody {Number} jump_address=1   跳转地址
 * @apiBody {Number} sort=1   排序
 * @apiParamExample {json} Request-Example:
 *     {
 *        "title": 1,
 *        "img_url": "10",
 *        "type": 1,
 *        "jump_address": 1,
 *        "sort": 1
 *     }
 * @apiGroup Nav
 * @apiVersion 1.0.0
 */
router.post("/nav/add", async function (req, res) {
  try {
    const time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    const { title, img_url, type, jump_address, sort } = req.body;
    const result = await Nav.create({
      title,
      img_url,
      type,
      jump_address,
      sort,
      create_time: time,
      updated_time: time,
    });
    console.log("添加配置", result);
    return res.json({ code: 200, message: "创建成功" });
  } catch (error) {
    console.log(error);
    return res.json({
      code: 500,
      message: "创建失败:" + err,
    });
  }
});

/**
 * @api {POST} /nav/del 轮播图删除
 * @apiDescription 轮播图删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   轮播图id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup Nav
 * @apiVersion 1.0.0
 */
router.post("/nav/del", async function (req, res) {
  try {
    const { id } = req.body;
    if (!id) {
      return res.send({ code: 500, msg: "缺少id参数" });
    }
    const result = await Nav.destroy({
      where: {
        id,
      },
    });
    console.log("配置列删除", result);
    return res.json({ code: 200, message: "删除成功" });
  } catch (error) {
    console.log(error);
    return res.json({
      code: 500,
      message: "创建失败:" + err,
    });
  }
});

/**
 * @api {POST} /nav/edit 轮播图编辑
 * @apiDescription 轮播图编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   轮播图id
 * @apiBody {String} title   标题
 * @apiBody {String} img_url   图片url
 * @apiBody {Number} type   图片类型
 * @apiBody {String} jump_address   跳转地址
 * @apiBody {Number} sort   排序
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *        "title": "",
 *        "img_url": 1,
 *        "type": "",
 *         "jump_address": 1,
 *        "sort": "",
 *     }
 * @apiGroup Nav
 * @apiVersion 1.0.0
 */
router.post("/nav/edit", async function (req, res) {
  try {
    const { id, title, img_url, type, jump_address, sort } = req.body;
    if (!id) {
      return res.send({ code: 500, msg: "缺少id参数" });
    }
    const one = await Nav.findOne({
      where: {
        id,
      },
    });
    console.log("查询配置列数据", one);
    if (!one) {
      return res.json({ code: 500, message: "轮播图列不存在" });
    }
    console.log("one", one);
    const data = one.dataValues;
    if (title) {
      data.title = title;
    }
    if (img_url) {
      data.img_url = img_url;
    }
    if (type) {
      data.type = type;
    }
    if (jump_address) {
      data.jump_address = jump_address;
    }
    if (sort) {
      data.sort = sort;
    }
    data.updated_time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    console.log("轮播图列保存数据", data);
    const result = await Nav.update(data, {
      where: {
        id,
      },
    });
    console.log("编辑轮播图列:", result);
    return res.json({ code: 200, message: "编辑成功" });
  } catch (error) {
    console.log(error);
    return res.json({
      code: 500,
      message: "创建失败:" + err,
    });
  }
});

module.exports = router;
